/**
 *
 */
package org.insia.smartposse.manager;

import org.hibernate.HibernateException;
import org.insia.smartposse.manager.iface.IManager;
import org.insia.smartposse.mapping.Application;
import org.insia.smartposse.mapping.Users;
import org.insia.smartposse.mapping.dao.ApplicationDAO;

/**
 * @author cmougel
 *
 */
public class ApplicationManager implements IManager {

	private final ApplicationDAO _dao = new ApplicationDAO();
	private final Application _app;

	public static final Integer ACCEPT = 1;
	public static final Integer REFUSED = -1;
	public static final Integer NODECIDED = 0;
	public static final Boolean FREELANCEFROM = true;
	public static final Boolean MANAGERFROM = false;

	public ApplicationManager(){
		_app = new Application();
	}
	public ApplicationManager(Integer id) {
		_app = _dao.load(id, _dao.getSession());
	}
	/**
	 * Define the Accept choice (must be -1, 0 or 1) see Static value
	 *
	 * @param choice
	 */
	public void setAccept(Integer choice){
		if (-1 > choice || 1 < choice){
			throw new HibernateException("The Acceptation must be -1, 0 or 1");
		}
		_app.setAccept(choice);
	}

	/**
	 * Say if this proposition is Accept
	 * @return
	 */
	public boolean isAccept(){
		return (ACCEPT == _app.getAccept());
	}

	/**
	 * Say if this proposition is Refused
	 * @return
	 */
	public boolean isRefused(){
		return (REFUSED == _app.getAccept());
	}

	/**
	 * Say if this proposition is Choice or not
	 * @return
	 */
	public boolean isChoice(){
		return (NODECIDED == _app.getAccept());
	}

	/**
	 * Define the Comment on a Freelance
	 *
	 * @param comment
	 */
	public void setCommentFreelance(String comment){
		_app.setCommentFreelance(comment);
	}

	/**
	 *
	 * @return The comment on a Freelance
	 */
	public String getCommentFreelance(){
		return _app.getCommentFreelance();
	}

	/**
	 * Define the comment on a Manager
	 *
	 * @param comment
	 */
	public void setCommentManager(String comment){
		_app.setCommentManager(comment);
	}

	/**
	 *
	 * @return the comment on a Manager
	 */
	public String getCommentManager(){
		return _app.getCommentManager();
	}

	/**
	 * Define the Freelance for this application
	 *
	 * @param freelance
	 */
	public void setFreelance(UserManager freelance){
		if (freelance.isFreelance()){
			_app.setFreelance(freelance.getUser());
		} else {
			throw new HibernateException("Only a freelance User");
		}
	}

	/**
	 *
	 * @return The Freelance for this application
	 */
	public UserManager getFreelance(){
		return new UserManager(_app.getFreelance());
	}

	/**
	 *
	 * @return The User From
	 */
	public UserManager getFrom(){
		return new UserManager(_app.getFrom());
	}

	/**
	 * Define the From of This Application. True if is the Freelance, false else.
	 * For define the From you must renseign the Freelance and JobProject before
	 *
	 * @param from
	 */
	public void setFrom(Boolean from){
		if (null != getFreelance() && null != getJobProject()){
			if (ApplicationManager.FREELANCEFROM == from){
				_app.setFrom(getFreelance().getUser());
			} else {
				_app.setFrom(getJobProject().getManager().getUser());
			}
		} else {
			throw new HibernateException("You must define a Freelance and a JobProject before define who is the from");
		}
	}
	public void setFrom(Users user) {
		_app.setFrom(user);
	}

	/**
	 * Define the Grade for the Freelance
	 *
	 * @param grade
	 */
	public void setGradeFreelance(Integer grade){
		_app.setGradeFreelance(grade);
	}

	/**
	 *
	 * @return The Grade of this Freelance
	 */
	public Integer getGradeFreelance(){
		return _app.getGradeFreelance();
	}

	/**
	 * Define the Grade for the Manager
	 *
	 * @param grade
	 */
	public void setGradeManager(Integer grade){
		_app.setGradeManager(grade);
	}

	/**
	 *
	 * @return The Grade of this Manager
	 */
	public Integer getGradeManager(){
		return _app.getGradeManager();
	}

	/**
	 *
	 * @return The message for this Application
	 */
	public String getMessage(){
		return _app.getMessage();
	}

	/**
	 * Define the message for this Application
	 *
	 * @param message
	 */
	public void setMessage(String message){
		_app.setMessage(message);
	}

	/**
	 *
	 * @return the JobProject for this Application
	 */
	public JobProjectManager getJobProject(){
		return new JobProjectManager(_app.getJobProject());
	}

	/**
	 * Define the JobProject
	 *
	 * @param jobProject
	 */
	public void setJobProject(JobProjectManager jobProject){
		_app.setJobProject(jobProject.getJobProject());
	}

	/**
	 * Define the Salary for this Application
	 *
	 * @param salary
	 */
	public void setSalary(Integer salary){
		_app.setSalary(salary);
	}

	/**
	 *
	 * @return The Salary for this Application
	 */
	public double getSalary(){
		return _app.getSalary();
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.manager.iface.IManager#save()
	 */
	public void save() {

		// No NULL in database
		if (null == _app.getCommentFreelance()){
			_app.setCommentFreelance("");
		}

		if (null == _app.getCommentManager()){
			_app.setCommentManager("");
		}

		if (null == _app.getMessage()){
			_app.setMessage("");
		}

		if (null == _app.getAccept()){
			_app.setAccept(0);
		}

		if (null == _app.getGradeManager()){
			_app.setGradeManager(0);
		}

		if (null == _app.getGradeFreelance()){
			_app.setGradeFreelance(0);
		}

		_dao.saveOrUpdate(_app);

	}
	public Integer getId() {
		return _app.getId();
	}



}
